<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是C语言基础——输入与输出相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-03-C语言-输入与输出">
<meta property="og:url" content="https://sumumm.github.io/post/b9a7171d.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是C语言基础——输入与输出相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220105210806637.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106100551934.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106101538008.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106115533901.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230614224539118.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230614224845558.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230614225324540.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106155842151.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615194016936.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615194621832.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106163053444.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615195721260.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615200824722.png">
<meta property="article:published_time" content="2022-01-12T08:29:55.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.984Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV01-C语言基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220105210806637.png">


<link rel="canonical" href="https://sumumm.github.io/post/b9a7171d.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/b9a7171d.html","path":"post/b9a7171d.html","title":"LV01-03-C语言-输入与输出"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-03-C语言-输入与输出 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%BE%93%E5%87%BA%E5%87%BD%E6%95%B0"><span class="nav-text">一、输出函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-putchar"><span class="nav-text">1. putchar </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">1.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-puts"><span class="nav-text">2. puts </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">2.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-printf"><span class="nav-text">3. printf </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">3.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AF%A6%E8%A7%A3"><span class="nav-text">3.2 格式化字符串详解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-%E6%A0%BC%E5%BC%8F%E5%AD%97%E7%AC%A6%EF%BC%88-specifier-%EF%BC%89"><span class="nav-text">3.2.1 格式字符（ specifier ）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-%E6%A0%87%E8%AF%86%EF%BC%88-flag-%EF%BC%89"><span class="nav-text">3.2.2 标识（ flag ）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-3-%E6%9C%80%E5%B0%8F%E8%BE%93%E5%87%BA%E5%AE%BD%E5%BA%A6%EF%BC%88-width-%EF%BC%89"><span class="nav-text">3.2.3 最小输出宽度（ width ）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-4-%E7%B2%BE%E5%BA%A6%EF%BC%88-precision-%EF%BC%89"><span class="nav-text">3.2.4 精度（ .precision ）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-5-%E9%95%BF%E5%BA%A6%EF%BC%88-length-%EF%BC%89"><span class="nav-text">3.2.5 长度（ length ）</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="nav-text">3.3 转义字符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-fprintf"><span class="nav-text">4. fprintf </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">4.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-sprintf"><span class="nav-text">5. sprintf </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="nav-text">5.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E8%BE%93%E5%85%A5%E5%87%BD%E6%95%B0"><span class="nav-text">二、输入函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-getchar"><span class="nav-text">1. getchar </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E-1"><span class="nav-text">1.1 函数说明 </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-gets"><span class="nav-text">2. gets </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E-1"><span class="nav-text">2.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-scanf"><span class="nav-text">3. scanf </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E-1"><span class="nav-text">3.1 函数说明 </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AF%A6%E8%A7%A3-1"><span class="nav-text">3.2 格式化字符串详解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1"><span class="nav-text">3.2.1 * </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-%E6%A0%BC%E5%BC%8F%E5%AD%97%E7%AC%A6-type"><span class="nav-text">3.2.2 格式字符( type )</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-3-%E6%95%B0%E6%8D%AE%E5%AE%BD%E5%BA%A6-width"><span class="nav-text">3.2.3 数据宽度( width )</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-4-%E4%BF%AE%E9%A5%B0%E7%AC%A6-modifiers"><span class="nav-text">3.2.4 修饰符( modifiers )</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E5%9C%B0%E5%9D%80%E8%A1%A8%E8%AF%A6%E8%A7%A3"><span class="nav-text">3.3 地址表详解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.4 使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-1-%E4%B8%80%E8%88%AC%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.4.1 一般使用实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-2-%E5%AD%97%E7%AC%A6%E8%BE%93%E5%85%A5%E6%97%B6%E7%9A%84%E7%A9%BA%E6%A0%BC"><span class="nav-text">3.4.2 字符输入时的空格</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-4-2-1-%E6%B5%8B%E8%AF%95%E6%BA%90%E7%A0%81"><span class="nav-text">3.4.2.1 测试源码</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-4-2-2-%E8%BF%9E%E7%BB%AD%E8%BE%93%E5%85%A5%E6%B5%8B%E8%AF%95"><span class="nav-text">3.4.2.2 连续输入测试</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-4-2-3-%E4%B8%AD%E9%97%B4%E5%B8%A6%E7%A9%BA%E6%A0%BC"><span class="nav-text">3.4.2.3 中间带空格</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-4-2-4-%E4%B8%AD%E9%97%B4%E5%B8%A6%E5%9B%9E%E8%BD%A6"><span class="nav-text">3.4.2.4 中间带回车</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-3-%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BE%93%E5%85%A5%E6%97%B6%E7%9A%84%E7%A9%BA%E6%A0%BC"><span class="nav-text">3.4.3 字符串输入时的空格</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-4-%E7%BC%93%E5%86%B2%E5%8C%BA%E7%9A%84%E5%A4%9A%E4%BD%99%E5%AD%97%E7%AC%A6"><span class="nav-text">3.4.4 缓冲区的多余字符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-5-%E6%9C%80%E5%90%8E%E7%9A%84%E6%8D%A2%E8%A1%8C%E7%9A%84%E5%A4%84%E7%90%86%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.4.5 最后的换行的处理实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-scanf-%E7%BC%93%E5%86%B2%E5%8C%BA%E5%A4%84%E7%90%86"><span class="nav-text">3.5 scanf 缓冲区处理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-1-getchar-%E5%A4%84%E7%90%86"><span class="nav-text">3.5.1 getchar 处理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-5-1-1-%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F"><span class="nav-text">3.5.1.1 处理方式</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-5-1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.5.1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-2-scanf-%E8%87%AA%E8%A1%8C%E5%A4%84%E7%90%86"><span class="nav-text">3.5.2 scanf 自行处理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-5-2-1-%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F"><span class="nav-text">3.5.2.1 处理方式</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-5-2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.5.2.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6-scanf-%E5%85%B6%E4%BB%96%E7%94%A8%E6%B3%95%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.6 scanf 其他用法实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-1-%E8%AF%BB%E5%8F%96%E5%B8%A6%E7%A9%BA%E6%A0%BC%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">3.6.1 读取带空格的字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-2-%E8%AF%BB%E5%8F%96%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E5%90%8E%E7%9A%84%E5%AD%97%E7%AC%A6"><span class="nav-text">3.6.2 读取特定字符后的字符</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-fscanf"><span class="nav-text">4. fscanf </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E-1"><span class="nav-text">4.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">4.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-sscanf"><span class="nav-text">5. sscanf </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E-1"><span class="nav-text">5.1 函数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">5.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E8%BE%93%E5%87%BA%E5%8F%AF%E4%BB%A5%E5%B8%A6%E9%A2%9C%E8%89%B2%EF%BC%9F"><span class="nav-text">三、输出可以带颜色？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-ANSI-%E6%8E%A7%E5%88%B6%E7%A0%81%E7%AE%80%E4%BB%8B"><span class="nav-text">1. ANSI 控制码简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-ANSI-%E6%8E%A7%E5%88%B6%E7%A0%81"><span class="nav-text">2. ANSI 控制码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-ANSI-%E7%A0%81%E5%BA%94%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">3. ANSI 码应用格式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-printf%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">3.1 printf使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.2 使用实例</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/b9a7171d.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-03-C语言-输入与输出 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-03-C语言-输入与输出
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-01-12 16:29:55" itemprop="dateCreated datePublished" datetime="2022-01-12T16:29:55+08:00">2022-01-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV01-C语言基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>11k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>41 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是C语言基础——输入与输出相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">cppreference</td><td align="left"><a href="https://en.cppreference.com/w/" target="_blank">https://en.cppreference.com/w/ <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Cplusplus</td><td align="left"><a target="_blank" rel="noopener" href="http://www.cplusplus.com/">http://www.cplusplus.com/ <i class="fa fa-external-link-alt"></i></a></td></tr>    <tr><td align="left">TutorialsPoint</td><td align="left"><a target="_blank" rel="noopener" href="https://www.tutorialspoint.com/index.htm">https://www.tutorialspoint.com/index.htm  <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<p>注意：下边测试输入的使用实例中的 ↙ 代表换行。</p>
<h1 id="一、输出函数"><a href="#一、输出函数" class="headerlink" title="一、输出函数"></a><font size=3>一、输出函数</font></h1><h2 id="1-putchar"><a href="#1-putchar" class="headerlink" title="1. putchar "></a><font size=3>1. putchar </font></h2><h3 id="1-1-函数说明"><a href="#1-1-函数说明" class="headerlink" title="1.1 函数说明"></a><font size=3>1.1 函数说明</font></h3><p>在 linux 下可以使用 man 3 putchar  命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">putchar</span><span class="params">(<span class="type">int</span> c)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> C 标准库 &lt;stdio.h&gt; 中的函数，把参数  c  指定的字符写入到标准输出  stdout  中。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>c  ： int 类型，这是要输出的字符，该字符以其对应的 int  值进行传递。</li>
</ul>
<p><strong>【返回值】</strong> int 类型，该函数以无符号  char  强制转换为  int  的形式返回写入的字符，如果发生错误则返回  EOF(-1) 。</p>
<p><strong>【使用格式】</strong> none </p>
<p><strong>【注意事项】</strong> putchar 函数只是输出一个字符，并不会自动换行。</p>
<h3 id="1-2-使用实例"><a href="#1-2-使用实例" class="headerlink" title="1.2 使用实例"></a><font size=3>1.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span> a = <span class="number">0x23</span>; <span class="comment">/* ascii 中 0x23 = # */</span></span><br><span class="line">    <span class="built_in">putchar</span>(a);    <span class="comment">/* 输出字符 */</span></span><br><span class="line">    <span class="built_in">putchar</span>(<span class="string">&#x27;\n&#x27;</span>); <span class="comment">/* 输出换行符 */</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-puts"><a href="#2-puts" class="headerlink" title="2. puts "></a><font size=3>2. puts </font></h2><h3 id="2-1-函数说明"><a href="#2-1-函数说明" class="headerlink" title="2.1 函数说明"></a><font size=3>2.1 函数说明</font></h3><p>在 linux 下可以使用 man 3 puts  命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">puts</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *s)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> C 标准库 &lt;stdio.h&gt; 中的函数，把一个字符串写入到标准输出  stdout ，直到空字符，但不包括空字符。换行符会被追加到输出中。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>str  ： char * 类型，这是要被写入的字符串。</li>
</ul>
<p><strong>【返回值】</strong> int 类型，如果成功，该函数返回一个非负值为字符串长度（包括末尾的  \0 ），如果发生错误则返回  EOF(-1)  。</p>
<p><strong>【使用格式】</strong> none </p>
<p><strong>【注意事项】</strong> puts 函数输出时会<strong>自动换行</strong>。</p>
<h3 id="2-2-使用实例"><a href="#2-2-使用实例" class="headerlink" title="2.2 使用实例"></a><font size=3>2.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">    <span class="type">char</span> str[<span class="number">16</span>] = <span class="string">&quot;abcdefg&quot;</span>;<span class="comment">/* 最后还有一个 \0 表示字符串的结束 */</span></span><br><span class="line">    i = <span class="built_in">puts</span>(str);    <span class="comment">/* 输出字符串 */</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;i=%d\n&quot;</span>,i);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-printf"><a href="#3-printf" class="headerlink" title="3. printf "></a><font size=3>3. printf </font></h2><p>该函数应该是应用最多的函数，用法也极其丰富。</p>
<h3 id="3-1-函数说明"><a href="#3-1-函数说明" class="headerlink" title="3.1 函数说明"></a><font size=3>3.1 函数说明</font></h3><p>在 linux 下可以使用 man 3 printf  命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"> <span class="type">int</span> <span class="title function_">printf</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *format, ...)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> C 标准库 &lt;stdio.h&gt; 中的函数，发送<strong>格式化输出</strong>到<strong>标准输出</strong>  stdout 。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>format ： char 类型，格式化字符串（ format  ），就是格式化需要输出的字符串， format  标签可被随后的附加参数中指定的值替换，并按需求进行格式化。</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看 format格式 </summary>
              <div class='content'>
              <ul><li>format 标签属性</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">%[flags][width][.precision][length]specifier</span><br><span class="line"><span class="comment">// 即</span></span><br><span class="line">%[标志][最小宽度][.精度][长度]类型。</span><br></pre></td></tr></table></figure><p>后边还会有更详细的说明</p>
              </div>
            </details>

<ul>
<li>… ：输出表（附加参数），根据不同的  format  字符串，函数可能需要一系列的附加参数，每个参数包含了一个要被插入的值，替换了  format  参数中指定的每个  %  标签。参数的个数应与 % 标签的个数相同。</li>
</ul>
<p><strong>【使用格式】</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;&lt;格式化字符串&gt;&quot;</span>, &lt;输出表&gt;);</span><br></pre></td></tr></table></figure>

<p><strong>【返回值】</strong> int 类型，如果成功，则返回写入的<strong>字符总数</strong>，否则返回一个负数。</p>
<p><strong>【注意事项】</strong> none </p>
<h3 id="3-2-格式化字符串详解"><a href="#3-2-格式化字符串详解" class="headerlink" title="3.2 格式化字符串详解"></a><font size=3>3.2 格式化字符串详解</font></h3><h4 id="3-2-1-格式字符（-specifier-）"><a href="#3-2-1-格式字符（-specifier-）" class="headerlink" title="3.2.1 格式字符（ specifier ）"></a><font size=3>3.2.1 格式字符（ specifier ）</font></h4><table>
    <tr>
        <td align="center" width=100px>格式字符</td>
        <td align="center">形式</td>
        <td align="center">意义</td>
    </tr>
    <tr>
        <td align="center" width=100px>i, d</td>
        <td align="center">%i, %d</td>
        <td align="left">以十进制形式输出带符号整数(正数不输出符号)</td>
    </tr>
    <tr>
        <td align="center" width=100px>u</td>
        <td align="center">%u</td>
        <td align="left">以十进制形式输出无符号整数</td>
    </tr>
    <tr>
        <td align="center" width=100px>o</td>
        <td align="center">%o</td>
        <td align="left">以八进制形式输出无符号整数(不输出前缀0)</td>
    </tr>
    <tr>
        <td align="center" width=100px>x, X</td>
        <td align="center">%x, %X</td>
        <td align="left">以十六进制形式输出无符号整数(不输出前缀0x)</td>
    </tr>
    <tr>
        <td align="center" width=100px>f</td>
        <td align="center">%f</td>
        <td align="left">以小数形式输出单、双精度实数</td>
    </tr>
    <tr>
        <td align="center" width=100px>e, E</td>
        <td align="center">%e, %E</td>
        <td align="left">以指数形式输出单、双精度实数</td>
    </tr>
    <tr>
        <td align="center" width=100px>g, G</td>
        <td align="center">%g, %G</td>
        <td align="left">以 %f 或 %e 中较短的输出宽度输出单、双精度实数</td>
    </tr>
    <tr>
        <td align="center" width=100px>c</td>
        <td align="center">%c</td>
        <td align="left">输出单个字符</td>
    </tr>
    <tr>
        <td align="center" width=100px>s</td>
        <td align="center">%s</td>
        <td align="left">输出字符串</td>
    </tr>
</table>
**【注意事项】**想要输出 % 本身的话可以使用 %% 。

<h4 id="3-2-2-标识（-flag-）"><a href="#3-2-2-标识（-flag-）" class="headerlink" title="3.2.2 标识（ flag ）"></a><font size=3>3.2.2 标识（ flag ）</font></h4><table>
    <tr>
        <td align="center" width=100px>标识</td>
        <td align="center">描述</td>
    </tr>
    <tr>
        <td align="center" width=100px>-</td>
        <td align="left">在给定的字段宽度内左对齐，默认是右对齐</td>
    </tr>
    <tr>
        <td align="center" width=100px>+</td>
        <td align="left">强制在结果之前显示加号或减号（+ 或 -），即正数前面会显示 + 号。默认情况下，只有负数前面会显示一个 - 号。</td>
    </tr>
    <tr>
        <td align="center" width=100px>space<br>(空格)</td>
        <td align="left">有符号值若为正，则在值前显示前导空格（但是不显示符号）；若为负，则在值前显示-。</td>
    </tr>
    <tr>
        <td align="center" width=100px rowspan="3">#</td>
        <td align="left">与 o、x 或 X 说明符一起使用时，非零值前面会分别显示 0、0x 或 0X。</td>
    </tr>
    <tr>
        <td align="left">与 e、E 和 f 一起使用时，会强制输出包含一个小数点，即使后边没有数字时也会显示小数点。默认情况下，如果后边没有数字时候，不会显示显示小数点。</td>
    </tr>
    <tr>
        <td align="left">与 g 或 G 一起使用时，结果与使用 e 或 E 时相同，但是尾部的零不会被移除。</td>
    </tr>
    <tr>
        <td align="center" width=100px>0</td>
        <td align="left">输出数值时指定左面不使用的空位置自动填0。
</td>
    </tr>
</table>

<h4 id="3-2-3-最小输出宽度（-width-）"><a href="#3-2-3-最小输出宽度（-width-）" class="headerlink" title="3.2.3 最小输出宽度（ width ）"></a><font size=3>3.2.3 最小输出宽度（ width ）</font></h4><table>
    <tr>
        <td align="center" width=100px>宽度</td>
        <td align="center">描述</td>
    </tr>
    <tr>
        <td align="center" width=100px>number</td>
        <td align="left">数值（十进制整数），数据长度 小于number，则左补空格；否则按实际输出。</td>
    </tr>
    <tr>
        <td align="center" width=100px>*</td>
        <td align="left">星号，精度在 format 字符串中未指定，但是会作为附加整数值参数放置于要被格式化的参数之前。<br>如：printf("%0*d",6,1000); 将输出:001000</td>
    </tr>
</table>

<h4 id="3-2-4-精度（-precision-）"><a href="#3-2-4-精度（-precision-）" class="headerlink" title="3.2.4 精度（ .precision ）"></a><font size=3>3.2.4 精度（ .precision ）</font></h4><table>
    <tr>
        <td align="center" width=100px>精度</td>
        <td align="center">描述</td>
    </tr>
    <tr><!-- 1 -->
        <td align="center" width=100px rowspan="6">.number</td>
        <td align="left">对于整数说明符（d、i、o、u、x、X）：precision 指定了要写入的数字的最小位数。如果写入的值短于该数，结果会用前导零来填充。如果写入的值长于该数，结果不会被截断。精度为 0 意味着不写入任何字符。</td>
    </tr>
    <tr><!-- 2 -->
        <td align="left">对于 e、E 和 f 说明符：要在小数点后输出的小数位数。</td>
    </tr>
    <tr><!-- 3 -->
        <td align="left">对于 g 和 G 说明符：要输出的最大有效位数。</td>
    </tr>
    <tr><!-- 4 -->
        <td align="left">对于 s : 要输出的最大字符数,不足number则正常输出，超过则截断。默认情况下，所有字符都会被输出，直到遇到末尾的空字符。</td>
    </tr>
    <tr><!-- 5 -->
        <td align="left">对于 c 类型：没有任何影响。</td>
    </tr>
    <tr><!-- 6 -->
        <td align="left">当未指定任何精度时，默认为 1。如果指定时不带有一个显式值，则假定为 0。</td>
    </tr>
    <tr>
        <td align="center" width=100px>.*</td>
        <td align="left">星号，精度在 format 字符串中未指定，但是会作为附加整数值参数放置于要被格式化的参数之前。<br>但是在尝试的时候似乎有一些数据类型不匹配的情况，不过也用的不多。</td>
    </tr>
</table>

<p>【注意事项】</p>
<p>1.精度格式符以  .  开头，后跟十进制整数。</p>
<p>2.对数值型数据的来说，未指定精度（ .precision ）时，隐含的精度为 6 位。</p>
<h4 id="3-2-5-长度（-length-）"><a href="#3-2-5-长度（-length-）" class="headerlink" title="3.2.5 长度（ length ）"></a><font size=3>3.2.5 长度（ length ）</font></h4><p>详情可查看该网站（含有 C99 标准）：<a target="_blank" rel="noopener" href="https://en.cppreference.com/w/c/io/fprintf">File input&#x2F;output。</a></p>
<h3 id="3-3-转义字符"><a href="#3-3-转义字符" class="headerlink" title="3.3 转义字符"></a><font size=3>3.3 转义字符</font></h3><table>
    <tr>
        <td align="center">转义字符</td>
        <td align="center">含义</td>
        <td align="center">ASCII码(十进制)</td>
    </tr>
    <tr>
        <td align="center">\a</td>
        <td align="left">响铃(BEL)</td>
        <td align="center">007</td>
    </tr>
    <tr>
        <td align="center">\b</td>
        <td align="left">退格(BS)，将当前位置移到前一列</td>
        <td align="center">008</td>
    </tr>
    <tr>
        <td align="center">\f</td>
        <td align="left">换页(FF)，将当前位置移到下页开头</td>
        <td align="center">012</td>
    </tr>
    <tr>
        <td align="center">\n</td>
        <td align="left">换行(LF)，将当前位置移到下一行开头</td>
        <td align="center">010</td>
    </tr>
    <tr>
        <td align="center">\r</td>
        <td align="left">回车(CR)，将当前位置移到本行开头</td>
        <td align="center">013</td>
    </tr>
    <tr>
        <td align="center">\t</td>
        <td align="left">水平制表(HT)，跳到下一个TAB位置</td>
        <td align="center">009</td>
    </tr>
    <tr>
        <td align="center">\v</td>
        <td align="left">垂直制表(VT)</td>
        <td align="center">011</td>
    </tr>
    <tr>
        <td align="center">\\</td>
        <td align="left">反斜线字符 \</td>
        <td align="center">092</td>
    </tr>
    <tr>
        <td align="center">\'</td>
        <td align="left">单引号字符 ' </td>
        <td align="center">039</td>
    </tr>
    <tr>
        <td align="center">\"</td>
        <td align="left">双引号字符 " </td>
        <td align="center">034</td>
    </tr>
    <tr>
        <td align="center">\?</td>
        <td align="left">问号 ？ </td>
        <td align="center">063</td>
    </tr>
    <tr>
        <td align="center">\0</td>
        <td align="left">空字符(NULL)</td>
        <td align="center">000</td>
    </tr>
    <tr>
        <td align="center">\ooo</td>
        <td align="left">1-3位八进制数所代表的任意字符</td>
        <td align="center">三位八进制</td>
    </tr>
    <tr>
        <td align="center">\xhh</td>
        <td align="left">1-2位十六进制数所代表的任意字符</td>
        <td align="center">二位十六进制</td>
    </tr>
</table>
### <font size=3>3.4 使用实例</font>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> c, str[<span class="number">20</span>]; </span><br><span class="line">	<span class="type">int</span> a = <span class="number">12345</span>;</span><br><span class="line">	<span class="type">float</span> pi = <span class="number">3.141592653589</span>; </span><br><span class="line">	<span class="type">double</span> b = <span class="number">0.123456789123456789</span>; </span><br><span class="line">	<span class="built_in">strcpy</span>(str, <span class="string">&quot;strings&quot;</span>);      <span class="comment">/* 将字符串复制到数组 */</span></span><br><span class="line">	c = <span class="string">&#x27;\x41&#x27;</span>;                  <span class="comment">/* 两位十六进制转移字符 代表A */</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %d\n&quot;</span>, a);       <span class="comment">/* 按照十进制整数格式输出，显示 a = 12345 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %d%%\n&quot;</span>, a);     <span class="comment">/* 输出%号 结果 a = 12345%             */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %7d\n&quot;</span>, a);      <span class="comment">/* 输出7位十进制整数 左边补空格，显示 a = 12345 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %07d\n&quot;</span>, a);     <span class="comment">/* 输出7位十进制整数 左边补0，显示 a = 0012345  */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %2d\n&quot;</span>, a);      <span class="comment">/* a超过2位，按实际输出 a = 12345             */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %-7d\n&quot;</span>, a);     <span class="comment">/* 输出7位十进制整数 右边补空格，显示 a = 12345  */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;pi = %f\n&quot;</span>, pi);     <span class="comment">/* 浮点数默认精度6位小数，结果 f = 13.141593    */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;pi = %6.4f\n&quot;</span>, pi);  <span class="comment">/* 输出6列，小数点后4位，结果 f = 3.1416        */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;b = %lf\n&quot;</span>, b);      <span class="comment">/* 输出长浮点数 b = 0.123457                  */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;b = %18.17lf\n&quot;</span>, b); <span class="comment">/* 输出18列，小数点后16位，b = 0.12345678912345678 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;c = %c\n&quot;</span>, c);       <span class="comment">/* 输出字符 c = A */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;c = %x\n&quot;</span>, c);       <span class="comment">/* 以十六进制输出字符的ASCII码 c = 41 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;str[] = %s\n&quot;</span>, str); <span class="comment">/* 输出数组字符串str[] = strings    */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;str[] = %6.3s\n&quot;</span>, str);<span class="comment">/* 输出最多3个字符的字符串 str[] = str */</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>打印结果如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220105210806637.png" alt="image-20220105210806637" style="zoom:50%;" />

<h2 id="4-fprintf"><a href="#4-fprintf" class="headerlink" title="4. fprintf "></a><font size=3>4. fprintf </font></h2><h3 id="4-1-函数说明"><a href="#4-1-函数说明" class="headerlink" title="4.1 函数说明"></a><font size=3>4.1 函数说明</font></h3><p>在 linux 下可以使用 man 3 fprintf 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">fprintf</span><span class="params">(FILE *stream, <span class="type">const</span> <span class="type">char</span> *format, ...)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数可以传送格式化输出到一个文件中，后边学习标准IO的时候会再学习，这里简单提一下。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>stream ： FILE * 类型指针变量，为文件指针变量。</li>
<li>format ： char * 类型，为格式化字符串，与 printf 一致。</li>
</ul>
<p><strong>【返回值】</strong> int 类型，成功时返回输出的字符个数；出错时返回 EOF 。</p>
<p><strong>【使用格式】</strong>一般情况下基本使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">FILE * fp;</span><br><span class="line"><span class="type">size_t</span> ret;</span><br><span class="line">fp = fopen(<span class="string">&quot;file_name&quot;</span>, <span class="string">&quot;w&quot;</span>);</span><br><span class="line">ret = <span class="built_in">fprintf</span>(fp, <span class="string">&quot;%d-%d-%d&quot;</span>, <span class="number">2022</span>, <span class="number">5</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure>

<p><strong>【注意事项】</strong>如果将  stream  设置为  stdout ，那么  fprintf()  函数将会向显示器输出内容，与  printf  的作用相同。</p>
<h3 id="4-2-使用实例"><a href="#4-2-使用实例" class="headerlink" title="4.2 使用实例"></a><font size=3>4.2 使用实例</font></h3><p>后边学习标准IO的时候会再详细学习。</p>
<h2 id="5-sprintf"><a href="#5-sprintf" class="headerlink" title="5. sprintf "></a><font size=3>5. sprintf </font></h2><h3 id="5-1-函数说明"><a href="#5-1-函数说明" class="headerlink" title="5.1 函数说明"></a><font size=3>5.1 函数说明</font></h3><p>在 linux 下可以使用 man 3 sprintf 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sprintf</span><span class="params">(<span class="type">char</span> *str, <span class="type">const</span> <span class="type">char</span> *format, ...)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数用于向指定的字符串中格式化写入数据，与 printf 类似，只是该函数将格式化的字符串写入到字符串中。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li><p>str ： char * 类型，为指向一个字符数组的指针，后边的格式化字符串将会被写入到该函数中。</p>
</li>
<li><p>format ： char * 类型，为格式化字符串，与 printf 一致。</p>
</li>
</ul>
<p><strong>【使用格式】</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> buff[<span class="number">100</span>]= &#123; <span class="number">0</span> &#125;;</span><br><span class="line"><span class="built_in">sprintf</span>(buff,<span class="string">&quot;%d-%d-%d&quot;</span>, <span class="number">2022</span>, <span class="number">5</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure>

<p><strong>【返回值】</strong> int 类型，如果成功，则返回写入的字符总数，不包括字符串追加在字符串末尾的空字符，如果失败，则返回 EOF 。</p>
<p><strong>【注意事项】</strong> none </p>
<h3 id="5-2-使用实例"><a href="#5-2-使用实例" class="headerlink" title="5.2 使用实例"></a><font size=3>5.2 使用实例</font></h3><p>后边学习标准IO再详细学习。</p>
<h1 id="二、输入函数"><a href="#二、输入函数" class="headerlink" title="二、输入函数"></a><font size=3>二、输入函数</font></h1><h2 id="1-getchar"><a href="#1-getchar" class="headerlink" title="1. getchar "></a><font size=3>1. getchar </font></h2><h3 id="1-1-函数说明-1"><a href="#1-1-函数说明-1" class="headerlink" title="1.1 函数说明 "></a><font size=3>1.1 函数说明 </font></h3><p>在 linux 下可以使用 man 3 getchar 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar</span><span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> C 标准库 &lt;stdio.h&gt; 中的函数，从标准输入  stdin  获取一个字符，比如我们从键盘输入字符 A ， B ,  C ，并按下回车后，我们的输入被放入了输入缓冲区，这个时候 getchar() 会从缓冲区中读取我们刚才的输入，一次只读取一个字符。</p>
<p><strong>【函数参数】</strong> none </p>
<p><strong>【使用格式】</strong> none </p>
<p><strong>【返回值】</strong> int 类型，该函数以无符号  char  强制转换为  int  的形式<strong>返回读取的字符</strong>，如果读取结束或发生读错误，则返回  EOF(-1) （输入 Ctrl+d ，就会返回  EOF ）。</p>
<p><strong>【注意事项】</strong></p>
<p>（1）通过该函数输入的都是字符，例如输入数字 1 ，实际输入的是字符 1 。</p>
<p>（2）以 Enter 结束输入（空格不会导致输入结束结束），他可以接受空格字符， Enter 结束输入时，接受空格，会舍弃最后的回车符。</p>
<h3 id="1-2-使用实例-1"><a href="#1-2-使用实例-1" class="headerlink" title="1.2 使用实例"></a><font size=3>1.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> a;</span><br><span class="line">	<span class="type">char</span> b;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入字符:&quot;</span>);</span><br><span class="line">	a = getchar();</span><br><span class="line">	b = <span class="keyword">sizeof</span>(a);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的字符:&quot;</span>);</span><br><span class="line">	<span class="built_in">putchar</span>(a);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %d, a = %x, b = %d\n&quot;</span>,a, a, b);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106100551934.png" alt="image-20220106100551934" style="zoom: 67%;" />

<h2 id="2-gets"><a href="#2-gets" class="headerlink" title="2. gets "></a><font size=3>2. gets </font></h2><h3 id="2-1-函数说明-1"><a href="#2-1-函数说明-1" class="headerlink" title="2.1 函数说明"></a><font size=3>2.1 函数说明</font></h3><p>在 linux 下可以使用 man  命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">char</span> *<span class="title function_">gets</span><span class="params">(<span class="type">char</span> *s)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> C 标准库 &lt;stdio.h&gt; 中的函数，从标准输入  stdin  读取一行（以 <Enter> 结束），并把它存储在  str  所指向的字符串中。当读取到换行符时，或者到达文件末尾时，它会停止，具体视情况而定。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>str ：  char * 类型，这是指向一个字符数组的指针，该数组存储了  C  字符串。</li>
</ul>
<p><strong>【使用格式】</strong> none </p>
<p><strong>【返回值】</strong> char * 类型，如果成功，该函数返回  str  。如果发生错误或者到缓冲区末尾时还未读取任何字符，则返回  NULL  。</p>
<p><strong>【注意事项】</strong></p>
<p>（1）使用该函数的时候会产生警告，这是因为在后来的标准中已经移除该函数，取而代之的是 fgets 。</p>
<p>（2） gets 从键盘输入一以<strong>回车结束</strong>的字符串放入<strong>字符数组</strong>中，并自动加  \0  ，需要注意的是输入字符串长度应小于字符数组维数，遇到回车时结束读取。</p>
<p>（2）与 scanf 函数不同， gets 函数并不以<strong>空格</strong>作为字符串输入结束的标志，它可以读取空格，遇到空格不会结束读取。</p>
<p>（3）可以读取 Tab 键，但是我测试的时候貌似把 tab 当做一个空格进行处理了。</p>
<h3 id="2-2-使用实例-1"><a href="#2-2-使用实例-1" class="headerlink" title="2.2 使用实例"></a><font size=3>2.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> str[<span class="number">50</span>];</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入字符串:&quot;</span>);</span><br><span class="line">	gets(str);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的字符串:&quot;</span>);</span><br><span class="line">	<span class="built_in">puts</span>(str);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;str[50] = %s,\n&quot;</span>, str);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106101538008.png" alt="image-20220106101538008" style="zoom:60%;" />

<h2 id="3-scanf"><a href="#3-scanf" class="headerlink" title="3. scanf "></a><font size=3>3. scanf </font></h2><h3 id="3-1-函数说明-1"><a href="#3-1-函数说明-1" class="headerlink" title="3.1 函数说明 "></a><font size=3>3.1 函数说明 </font></h3><p>在 linux 下可以使用 man  命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">scanf</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *format, ...)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> C 标准库 &lt;stdio.h&gt; 中的函数，从标准输入  stdin  读取格式化输入，它不会读取空格、制表符、回车符，这些符号将会留在缓冲区中。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>format ： char * 类型，格式化字符串（ format  ），这是  C  字符串，包含了以下各项中的一个或多个：<strong>空格字符、非空格字符 和  format  说明符</strong>。</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看 format 格式 </summary>
              <div class='content'>
              <ul><li>format  说明符形式为: %[*][width][modifiers]type</li></ul><table>    <tr>        <td align="center">参数</td>        <td align="center">描述</td>    </tr>    <tr>        <td align="center">*</td>        <td align="left">这是一个可选的星号，表示数据是从流 stream 中读取的，但是可以被忽视，即它不存储在对应的参数中。</td>    </tr>    <tr>        <td align="center">width</td>        <td align="left">这指定了在当前读取操作中读取的最大字符数。</td>    </tr>    <tr>        <td align="center">modifiers</td>        <td align="left">为对应的附加参数所指向的数据指定一个不同于整型（针对 d、i 和 n）、无符号整型（针对 o、u 和 x）或浮点型（针对 e、f 和 g）的大小。</td>    </tr>    <tr>        <td align="center">type</td>        <td align="left">一个字符，指定了要被读取的数据类型以及数据读取方式。具体参见下一个表格。</td>	    </tr></table>
              </div>
            </details>


<ul>
<li>… ：附加参数（地址表），根据不同的  format  字符串，函数可能需要一系列的附加参数，每个参数包含了一个要被插入的值，替换了  format  参数中指定的每个  % 标签。参数的个数应与  %  标签的个数相同。</li>
</ul>
<p><strong>【使用格式】</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(<span class="string">&quot;&lt;格式化字符串&gt;&quot;</span>, &lt;地址表&gt;);</span><br></pre></td></tr></table></figure>

<p><strong>【返回值】</strong> int 类型，如果成功，该函数返回成功匹配和赋值的个数。如果到达文件末尾或发生读错误，则返回  EOF(-1)  。</p>
<p><strong>【注意事项】</strong></p>
<p>（1） scanf 读取结束的标志：</p>
<ul>
<li>&lt;space&gt; 、 Tab 、或 Enter 。</li>
<li>遇非法输入。</li>
<li>遇宽度结束（就是输入的时候有类似  %4d ，这种的话，要是我们输入 212345 ，那么它只会读取 4 个字符）。</li>
</ul>
<p>（2）连续输入字符型变量（即使用 %c ）的时候，空格和转义字符会作为有效字符输入（后边会有详细的实例说明）。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(“%c%c%c”,&amp;c1, &amp;c2, &amp;c3);</span><br><span class="line">若输入a b c   </span><br><span class="line">则c1为a, c2为空格, c3为b</span><br></pre></td></tr></table></figure>

<h3 id="3-2-格式化字符串详解-1"><a href="#3-2-格式化字符串详解-1" class="headerlink" title="3.2 格式化字符串详解"></a><font size=3>3.2 格式化字符串详解</font></h3><h4 id="3-2-1"><a href="#3-2-1" class="headerlink" title="3.2.1 * "></a><font size=3>3.2.1 * </font></h4><p>基本没有用过，这里不再说明，以后用到了再补充。</p>
<h4 id="3-2-2-格式字符-type"><a href="#3-2-2-格式字符-type" class="headerlink" title="3.2.2 格式字符( type )"></a><font size=3>3.2.2 格式字符( type )</font></h4><table>
    <tr>
        <td align="center" width=100px>格式字符</td>
        <td align="center" width=100px>形式</td>
        <td align="center">意义</td>
    </tr>
    <tr>
        <td align="center" width=100px>i, d</td>
        <td align="center" width=100px>%i, %d</td>
        <td align="left">输入或读取十进制整数，数字前面的 + 或 - 号是可选的。</td>
    </tr>
    <tr>
        <td align="center" width=100px>u</td>
        <td align="center" width=100px>%u</td>
        <td align="left">输入或读取十进制无符号整数。</td>
    </tr>
    <tr>
        <td align="center" width=100px>o</td>
        <td align="center" width=100px>%o</td>
        <td align="left">输入或读取八进制无符号整数。</td>
    </tr>
    <tr>
        <td align="center" width=100px>x, X</td>
        <td align="center" width=100px>%x, %X</td>
        <td align="left">输入或读取十六进制无符号整数。</td>
    </tr>
    <tr>
        <td align="center" width=100px>f, F</td>
        <td align="center" width=100px>%f, %F</td>
        <td align="left" rowspan="3">输入或读取小数形式(指数形式浮点小数
)浮点数。</td>
    </tr>
    <tr>
        <td align="center" width=100px>e, E</td>
        <td align="center" width=100px>%e, %E</td>
    </tr>
    <tr>
        <td align="center" width=100px>g, G</td>
        <td align="center" width=100px>%g, %G</td>
    </tr>
    <tr>
        <td align="center" width=100px>c</td>
        <td align="center" width=100px>%c</td>
        <td align="left">输入或读取单个字符。空格和转义字符作为有效字符输入。</td>
    </tr>
    <tr>
        <td align="center" width=100px>s</td>
        <td align="center" width=100px>%s</td>
        <td align="left">输入或读取字符串，这将读取连续字符，直到遇到一个空格字符（空格字符可以是空白、换行和制表符）。</td>
    </tr>
</table>

<p><strong>【注意事项】</strong></p>
<p>（1）输入 double 类型数据必须用 lf ，不能用 f 。</p>
<p>（2）输入 % ，可以用 %% 。</p>
<h4 id="3-2-3-数据宽度-width"><a href="#3-2-3-数据宽度-width" class="headerlink" title="3.2.3 数据宽度( width )"></a><font size=3>3.2.3 数据宽度( width )</font></h4><table>
    <tr>
        <td align="center" width=100px>参数</td>
        <td align="center">描述</td>
    </tr>
    <tr>
        <td align="center">number</td>
        <td align="left">指定输入数据宽度，遇空格或不可转换字符结束。</td>
    </tr>
</table>

<h4 id="3-2-4-修饰符-modifiers"><a href="#3-2-4-修饰符-modifiers" class="headerlink" title="3.2.4 修饰符( modifiers )"></a><font size=3>3.2.4 修饰符( modifiers )</font></h4><table>
    <tr>
        <td align="center" width=100px>参数</td>
        <td align="center">描述</td>
    </tr>
    <tr>
        <td align="center">h</td>
        <td align="left">用于d, o, x前，指定输入为short型整数。</td>
    </tr>
    <tr>
        <td align="center" rowspan="2">l</td>
        <td align="left">用于d, o, x前，指定输入为long型整数。</td>
    </tr>
    <tr>
        <td align="left">用于e, f前，指定输入为double型实数。</td>
    </tr>
    <tr>
        <td align="center">*</td>
        <td align="left">抑制符，指定输入项读入后不赋给变量。</td>	
    </tr>
</table>


<h3 id="3-3-地址表详解"><a href="#3-3-地址表详解" class="headerlink" title="3.3 地址表详解"></a><font size=3>3.3 地址表详解</font></h3><p> scanf 函数读取时，后边的地址表代表的是地址，也就是说，必须是地址的形式。</p>
<table>
    <tr>
        <td align="center" width=100px>类型</td>
        <td align="left">说明</td>
    </tr>
    <tr>
        <td align="center" width=100px>变量</td>
        <td align="left">对于变量，应该加上 & 符号，如：&a, &b等。</td>
    </tr>
    <tr>
        <td align="center" width=100px>数组</td>
        <td align="left">对于数组，数组名就代表了该数组的起始地址，所以不需要加 & 。如：a[12]&rarr;a即可。</td>
    </tr>
    <tr>
        <td align="center" width=100px>指针</td>
        <td align="left">对于指针，指针名就代表了该指针指向的地址，所以不需要加 & 。如： *p &rarr; p 即可。</td>
    </tr>
</table>



<h3 id="3-4-使用实例"><a href="#3-4-使用实例" class="headerlink" title="3.4 使用实例"></a><font size=3>3.4 使用实例</font></h3><h4 id="3-4-1-一般使用实例"><a href="#3-4-1-一般使用实例" class="headerlink" title="3.4.1 一般使用实例"></a><font size=3>3.4.1 一般使用实例</font></h4><ul>
<li>格式字符串必须严格按书写格式输入</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a, a1, a2;</span><br><span class="line">	<span class="type">int</span> b, b1, b2;</span><br><span class="line">	<span class="type">int</span> c, c1, c2;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d%d%d&quot;</span>,&amp;a, &amp;b, &amp;c);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:a = %d, b = %d, c = %d\n&quot;</span>, a, b, c);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d,%d,%d&quot;</span>,&amp;a1, &amp;b1, &amp;c1);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:a1 = %d, b1 = %d, c1 = %d\n&quot;</span>, a1, b1, c1);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d %d&quot;</span>,&amp;a2, &amp;b2, &amp;c2);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:a2 = %d, b2 = %d, c2 = %d\n&quot;</span>, a2, b2, c2);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行效果：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106115533901.png" alt="image-20220106115533901" style="zoom:67%;" />

<p><strong>【注意事项】</strong></p>
<p>（1） scanf 中的格式字符串中除了 % 部分的格式字符，其他的自行添加的（如 , 、其他的字母等）在输入的时候要严格按照格式一起输入，否则输入的数据会有问题。经实验证明，<strong>格式字符串中的空格</strong>（并不是从终端输入的时候的字符串）似乎不会对输入结果产生影响。</p>
<p>（2）在同时进行多个数字变量的数据输入时，相邻数据之间可以用 &lt;space&gt; 、 &lt;Tab&gt; 或者 &lt;Enter&gt; 均可，但要注意自己是否添加了其他字符。</p>
<h4 id="3-4-2-字符输入时的空格"><a href="#3-4-2-字符输入时的空格" class="headerlink" title="3.4.2 字符输入时的空格"></a><font size=3>3.4.2 字符输入时的空格</font></h4><h5 id="3-4-2-1-测试源码"><a href="#3-4-2-1-测试源码" class="headerlink" title="3.4.2.1 测试源码"></a><font size=3>3.4.2.1 测试源码</font></h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> a, b, c, d;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入字符(a b c):&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%c%c%c&quot;</span>,&amp;a, &amp;b, &amp;c);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的字符为:a = %c(%#x), b = %c(%#x), c = %c(%#x)\n&quot;</span>, a, a, b, b, c, c);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入字符(d):&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%c&quot;</span>,&amp;d);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的字符为:d = %c(%#x)\n&quot;</span>, d, d);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>【注意事项】</strong></p>
<p>（1）在用 %c 进行单个字符输入时，<strong>空格和转义字符作为有效字符输入</strong>。</p>
<p>（2）在测试过程中，若有连续两个 scanf 函数进行输入时，上一个 scanf 多余出来的数据（<strong>包括最后的 &lt;Enter&gt;</strong> ）将会直接作为下一个 scanf 的输入。</p>
<h5 id="3-4-2-2-连续输入测试"><a href="#3-4-2-2-连续输入测试" class="headerlink" title="3.4.2.2 连续输入测试"></a><font size=3>3.4.2.2 连续输入测试</font></h5><p>我们这样输入数据：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hk@ubuntu<span class="number">-22</span><span class="number">-04</span>:~/<span class="number">1</span>sharedfiles/<span class="number">3L</span>inux/<span class="number">01</span>-LV1/<span class="number">04</span>输入输出$ ./a.out </span><br><span class="line">请输入字符(a b c):<span class="number">123</span>↙</span><br></pre></td></tr></table></figure>

<p>然后我们就会得到这样的输出数据：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230614224539118.png" alt="image-20230614224539118" style="zoom:50%;" />



<p>我们查阅一下ASCII码表就会发现，0xa其实就是表示 \n ，也就是换行。上边的现象就说明，换行符号也被当做一个字符，并且直接被当做下一个scanf的输入了。</p>
<h5 id="3-4-2-3-中间带空格"><a href="#3-4-2-3-中间带空格" class="headerlink" title="3.4.2.3 中间带空格"></a><font size=3>3.4.2.3 中间带空格</font></h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hk@ubuntu<span class="number">-22</span><span class="number">-04</span>:~/<span class="number">1</span>sharedfiles/<span class="number">3L</span>inux/<span class="number">01</span>-LV1/<span class="number">04</span>输入输出$ ./a.out </span><br><span class="line">请输入字符(a b c):<span class="number">1</span> <span class="number">23</span>↙</span><br></pre></td></tr></table></figure>

<p>我们会得到以下输出信息：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230614224845558.png" alt="image-20230614224845558" style="zoom:50%;" />

<p>可以看到，空格被赋给了b，最后的3则直接赋给了d。</p>
<h5 id="3-4-2-4-中间带回车"><a href="#3-4-2-4-中间带回车" class="headerlink" title="3.4.2.4 中间带回车"></a><font size=3>3.4.2.4 中间带回车</font></h5><p>我们输入完12后，直接输入一个回车，根据前边的现象，a会被赋值为1，b会被赋值为2，c会被赋值为 0xa，然后由于缓冲区数据了，所以当遇到第二个scanf的时候会提示我们输入字符：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hk@ubuntu<span class="number">-22</span><span class="number">-04</span>:~/<span class="number">1</span>sharedfiles/<span class="number">3L</span>inux/<span class="number">01</span>-LV1/<span class="number">04</span>输入输出$ ./a.out </span><br><span class="line">请输入字符(a b c):<span class="number">12</span>↙</span><br></pre></td></tr></table></figure>

<p>我们会看到这样的输出：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230614225324540.png" alt="image-20230614225324540" style="zoom:50%;" />

<h4 id="3-4-3-字符串输入时的空格"><a href="#3-4-3-字符串输入时的空格" class="headerlink" title="3.4.3 字符串输入时的空格"></a><font size=3>3.4.3 字符串输入时的空格</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> str1[<span class="number">20</span>];</span><br><span class="line">	<span class="type">char</span> str2[<span class="number">20</span>];</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入字符串:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,str1);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的字符串为:str[20] = %s\n&quot;</span>, str1);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入字符串:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,str2);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的字符串为:str[20] = %s\n&quot;</span>, str2);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106155842151.png" alt="image-20220106155842151" style="zoom:67%;" />

<p><strong>【注意事项】</strong></p>
<p>（1） %s 进行字符串输入时，遇到 &lt;space&gt; 即认为该数据结束。</p>
<p>（2）若有连续两个 scanf 函数进行输入时，上一个 scanf 输入的数据若有 &lt;space&gt; ，则 &lt;space&gt; 后的数据，将会直接作为下一个 scanf 的输入。</p>
<h4 id="3-4-4-缓冲区的多余字符"><a href="#3-4-4-缓冲区的多余字符" class="headerlink" title="3.4.4 缓冲区的多余字符"></a><font size=3>3.4.4 缓冲区的多余字符</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> y,m,d;</span><br><span class="line">	<span class="type">int</span> y1,m1,d1;</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	<span class="type">char</span> b;</span><br><span class="line">	<span class="type">float</span> c;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%4d%2d%2d&quot;</span>, &amp;y, &amp;m, &amp;d);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:y = %d, m = %d, d = %d\n&quot;</span>, y, m, d);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%4d%3d%2d&quot;</span>, &amp;y1, &amp;m1, &amp;d1);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:y1 = %d, m1 = %d, d1 = %d\n&quot;</span>, y1, m1, d1);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d%c%f&quot;</span>,&amp;a,&amp;b,&amp;c);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:a = %d, b = %c, c = %f\n&quot;</span>, a, b, c);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们这样输入数据,</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">hk@ubuntu<span class="number">-22</span><span class="number">-04</span>:~/<span class="number">1</span>sharedfiles/<span class="number">3L</span>inux/<span class="number">01</span>-LV1/<span class="number">04</span>输入输出$ ./a.out </span><br><span class="line">请输入数据:<span class="number">20230615</span>↙</span><br><span class="line">输入的数据为:y = <span class="number">2023</span>, m = <span class="number">6</span>, d = <span class="number">15</span></span><br><span class="line">请输入数据:<span class="number">2023061512</span>↙</span><br><span class="line">输入的数据为:y1 = <span class="number">2023</span>, m1 = <span class="number">61</span>, d1 = <span class="number">51</span></span><br><span class="line">请输入数据:<span class="number">2021</span>a12310<span class="number">.315</span>↙</span><br><span class="line">输入的数据为:a = <span class="number">2</span>, b = </span><br><span class="line">, c = <span class="number">2021.000000</span></span><br></pre></td></tr></table></figure>

<p>实际效果如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615194016936.png" alt="image-20230615194016936" style="zoom:50%;" />

<p>这里我们直接分析一下：</p>
<p>第一次输入：20230615↙，这个时候我们需要按%4d%2d%2d的格式读取数据，这样会以此得到y&#x3D;2023，m&#x3D;06，d&#x3D;15，这是没问题的。</p>
<p>第二次输入：2023061512↙，这次我们按照%4d%3d%2d的格式读取，会得到y1&#x3D;2023，m1&#x3D;061，d1&#x3D;51，这也是没问题的，这个时候其实缓冲区还剩下2这个数字。</p>
<p>第三次输入：2021a12310.315↙，我们按照%d%c%f的格式来读取，理论上我们应该获取到什么数据？我们实际验证一下吧：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	<span class="type">char</span> b;</span><br><span class="line">	<span class="type">float</span> c;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d%c%f&quot;</span>,&amp;a,&amp;b,&amp;c);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:a = %d, b = %c, c = %f\n&quot;</span>, a, b, c);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们同样输入2021a12310.315↙，会得到以下输出：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hk@ubuntu<span class="number">-22</span><span class="number">-04</span>:~/<span class="number">1</span>sharedfiles/<span class="number">3L</span>inux/<span class="number">01</span>-LV1/<span class="number">04</span>输入输出$ ./a.out </span><br><span class="line">请输入数据:<span class="number">2021</span>a12310<span class="number">.315</span>↙</span><br><span class="line">输入的数据为:a = <span class="number">2021</span>, b = a, c = <span class="number">12310.315430</span></span><br></pre></td></tr></table></figure>

<p>可是上边我们实际得到的是：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615194621832.png" alt="image-20230615194621832" style="zoom:50%;" />

<p>这是为什么？原因就在于我们第二次输入的时候，缓冲区还剩下一个2和 ↙（换行），它俩直接作为最后一个scanf的输入，所以最后依次输入的时候scanf只会读取一个%f格式的数据，由于2021后边是字符a，所以%f就读取到了2021，最后做了强制类型转换，就输出了上边的数据啦。</p>
<p><strong>【注意事项】</strong></p>
<p>（1）输入数据时，要格外注意遇以下情况认为该数据结束：遇 &lt;space&gt; 、 Tab 、或 Enter ；遇非法输入；遇宽度结束。</p>
<p>（2）一定要注意上一次的数据输入是否还有余留，可能会导致后续输入出现问题，具体处理方法看后文。</p>
<h4 id="3-4-5-最后的换行的处理实例"><a href="#3-4-5-最后的换行的处理实例" class="headerlink" title="3.4.5 最后的换行的处理实例"></a><font size=3>3.4.5 最后的换行的处理实例</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> x;</span><br><span class="line">	<span class="type">char</span> ch;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;x);</span><br><span class="line">	<span class="comment">// getchar();     /* 加上此行，吸收 &lt;Enter&gt; 或者可以用 scanf(&quot;%*c%c&quot;, &amp;x); */</span></span><br><span class="line">	ch = getchar();</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;x = %d, ch=%d\n&quot;</span>, x, ch); <span class="comment">/* \n 的十进制为 10 */</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20220106163053444.png" alt="image-20220106163053444" style="zoom:67%;" />

<h3 id="3-5-scanf-缓冲区处理"><a href="#3-5-scanf-缓冲区处理" class="headerlink" title="3.5 scanf 缓冲区处理"></a><font size=3>3.5 scanf 缓冲区处理</font></h3><p>很多时候，我们输入了一串数据后，又继续使用 scanf 输入，但此时缓冲区可能会有上次遗留下来的其他字符，包括换行符，这会对之后的输入造成影响的。我们就用前边《缓冲区的多余字符》一小节的笔记中的例子来做对比</p>
<h4 id="3-5-1-getchar-处理"><a href="#3-5-1-getchar-处理" class="headerlink" title="3.5.1 getchar 处理"></a><font size=3>3.5.1 getchar 处理</font></h4><h5 id="3-5-1-1-处理方式"><a href="#3-5-1-1-处理方式" class="headerlink" title="3.5.1.1 处理方式"></a><font size=3>3.5.1.1 处理方式</font></h5><p>我们可以使用 getchar() 来吸收缓冲区剩下的字符：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> ch;</span><br><span class="line"><span class="keyword">while</span> ((ch = getchar()) != EOF &amp;&amp; ch != <span class="string">&#x27;\n&#x27;</span>) ; <span class="comment">//清除缓冲区的内容</span></span><br></pre></td></tr></table></figure>

<p><strong>【说明】</strong>输入流中多余的字符均可通过此行代码处理。</p>
<h5 id="3-5-1-2-使用实例"><a href="#3-5-1-2-使用实例" class="headerlink" title="3.5.1.2 使用实例"></a><font size=3>3.5.1.2 使用实例</font></h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> y,m,d;</span><br><span class="line">	<span class="type">int</span> y1,m1,d1;</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	<span class="type">char</span> b;</span><br><span class="line">	<span class="type">float</span> c;</span><br><span class="line">	<span class="type">char</span> ch;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%4d%2d%2d&quot;</span>, &amp;y, &amp;m, &amp;d);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:y = %d, m = %d, d = %d\n&quot;</span>, y, m, d);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> ((ch = getchar()) != EOF &amp;&amp; ch != <span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%4d%3d%2d&quot;</span>, &amp;y1, &amp;m1, &amp;d1);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:y1 = %d, m1 = %d, d1 = %d\n&quot;</span>, y1, m1, d1);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> ((ch = getchar()) != EOF &amp;&amp; ch != <span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line">    </span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d%c%f&quot;</span>,&amp;a,&amp;b,&amp;c);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:a = %d, b = %c, c = %f\n&quot;</span>, a, b, c);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后我们按照相同的方式输入数据，会得到以下输出：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615195721260.png" alt="image-20230615195721260" style="zoom:50%;" />

<p>可以发现输入都正常了，但是吧最后的浮点型数据多了个430，，目前还不是很清楚为什么，不过问题不大，感觉像是打印的时候没有保留小数位数，应该算是多打印了内存中的一些其他数据，其实可以自己通过格式化字符串的时候那个精度来截断一下就好了。</p>
<h4 id="3-5-2-scanf-自行处理"><a href="#3-5-2-scanf-自行处理" class="headerlink" title="3.5.2 scanf 自行处理"></a><font size=3>3.5.2 scanf 自行处理</font></h4><h5 id="3-5-2-1-处理方式"><a href="#3-5-2-1-处理方式" class="headerlink" title="3.5.2.1 处理方式"></a><font size=3>3.5.2.1 处理方式</font></h5><p>我们还可以用 scanf 自己来处理：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(<span class="string">&quot;%*[^\n]%*c&quot;</span>);<span class="comment">/* 遇到&#x27;\n&#x27;结束读取输入的字符,并将其读到的数据抛弃,然后再抛弃一个字符（其实这个字符是&#x27;\n&#x27;），此时缓存中不存在任何字符 */</span></span><br></pre></td></tr></table></figure>

<ul>
<li>* ：这里的星号  *  表示读入某类型的内容，但是这个内容不保存到变量里，所以后面不需要对应的参量；</li>
<li>[] ：里边的内容是只读入限定读入的字符，如 [abcd] 指的是只读入 abcd 的字符；</li>
<li>%*[^\n] ：表示读入除了回车之外的字符以及读入一个字符后不保存，只有这样，才不会把输入回车吸收，导致不能退出程序；</li>
<li>%*c ：是读入最后一个没有读入的回车；</li>
</ul>
<h5 id="3-5-2-2-使用实例"><a href="#3-5-2-2-使用实例" class="headerlink" title="3.5.2.2 使用实例"></a><font size=3>3.5.2.2 使用实例</font></h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> y,m,d;</span><br><span class="line">	<span class="type">int</span> y1,m1,d1;</span><br><span class="line">	<span class="type">int</span> a;</span><br><span class="line">	<span class="type">char</span> b;</span><br><span class="line">	<span class="type">float</span> c;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%4d%2d%2d%*[^\n]%*c&quot;</span>, &amp;y, &amp;m, &amp;d);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:y = %d, m = %d, d = %d\n&quot;</span>, y, m, d);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%4d%3d%2d%*[^\n]%*c&quot;</span>, &amp;y1, &amp;m1, &amp;d1);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:y1 = %d, m1 = %d, d1 = %d\n&quot;</span>, y1, m1, d1);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入数据:&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d%c%f%*[^\n]%*c&quot;</span>,&amp;a,&amp;b,&amp;c);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的数据为:a = %d, b = %c, c = %f\n&quot;</span>, a, b, c);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>按照前边一样的输入方式，可以得到以下输出情况：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-03-C%E8%AF%AD%E8%A8%80-%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA/img/image-20230615200824722.png" alt="image-20230615200824722" style="zoom:50%;" />

<p>可以发现获取到的数据也都正常了。</p>
<h3 id="3-6-scanf-其他用法实例"><a href="#3-6-scanf-其他用法实例" class="headerlink" title="3.6 scanf 其他用法实例"></a><font size=3>3.6 scanf 其他用法实例</font></h3><h4 id="3-6-1-读取带空格的字符串"><a href="#3-6-1-读取带空格的字符串" class="headerlink" title="3.6.1 读取带空格的字符串"></a><font size=3>3.6.1 读取带空格的字符串</font></h4><p>我们使用 scanf 的话，也想读取带空格的字符串的话，可以使用这样的格式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(<span class="string">&quot;%[^\n]%*c&quot;</span>,str); <span class="comment">/* 遇到&#x27;\n&#x27;结束读取输入的字符,并存入str,然后再抛弃一个字符 */</span></span><br></pre></td></tr></table></figure>

<p>例如，</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> str[<span class="number">30</span>];</span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;请输入字符串:&quot;</span>);</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%[^\n]%*c&quot;</span>, str); <span class="comment">//读到&#x27;\n&#x27;结束读取</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;输入的字符串为:%s\n&quot;</span>, str);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>%*c ：是读入最后一个没有读入的回车</li>
</ul>
<p>【注意事项】后边不加 %*c 的话，依旧可以读取带空格的字符串，但是若是在一个循环中的话，会陷入死循环，至少我测试的时候是这样的，比如说上边的例子，若没有 %*c 的话，就会一直执行这一句</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;输入的字符串为:%s\n&quot;</span>, str);</span><br></pre></td></tr></table></figure>

<h4 id="3-6-2-读取特定字符后的字符"><a href="#3-6-2-读取特定字符后的字符" class="headerlink" title="3.6.2 读取特定字符后的字符"></a><font size=3>3.6.2 读取特定字符后的字符</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> ch;  </span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">&quot;%*[^a]%*c%c&quot;</span>,&amp;ch); <span class="comment">/* 遇到字符a停止读取，并舍弃a, 将a后的一个字符存入变量ch */</span></span><br></pre></td></tr></table></figure>

<p>例如，</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> ch;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;请输入字符:&quot;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%*[^a]%*c%c&quot;</span>, &amp;ch); <span class="comment">/* 遇到字符a停止读取，并舍弃a, 将a后的一个字符存入变量ch */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;输入的字符为:%c\n&quot;</span>, ch);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们编译运行的话，会有以下情况</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">请输入字符:sdaddsa sa d</span><br><span class="line">输入的字符为:d</span><br></pre></td></tr></table></figure>

<h2 id="4-fscanf"><a href="#4-fscanf" class="headerlink" title="4. fscanf "></a><font size=3>4. fscanf </font></h2><h3 id="4-1-函数说明-1"><a href="#4-1-函数说明-1" class="headerlink" title="4.1 函数说明"></a><font size=3>4.1 函数说明</font></h3><p>在 linux 下可以使用 man 3 fscanf 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">fscanf</span><span class="params">(FILE *stream, <span class="type">const</span> <span class="type">char</span> *format, ...)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数从流 stream  读取格式化输入，即从文件格式化读取数据，后边学习标准IO的时候会再详细学习。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>stream ： FILE * 类型，为文件指针变量。</li>
<li>format ： char * 类型，为格式化字符串，与 scanf 一致。</li>
</ul>
<p><strong>【返回值】</strong> int 类型，如果成功，该函数返回成功匹配和赋值的个数，如果到达文件末尾或发生读错误，则返回  EOF 。</p>
<p><strong>【使用格式】</strong>一般情况下基本使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">FILE * fp;</span><br><span class="line"><span class="type">int</span> year, month, day;</span><br><span class="line">fp = fopen(<span class="string">&quot;file_name&quot;</span>, <span class="string">&quot;r&quot;</span>);</span><br><span class="line"><span class="built_in">fscanf</span>(fp,<span class="string">&quot;%d-%d-%d&quot;</span>,&amp;year, &amp;month, &amp;day);</span><br></pre></td></tr></table></figure>

<p><strong>【注意事项】</strong>如果将  fp  设置为  stdin ，那么  fscanf()  函数将会从键盘读取数据，与  scanf  的作用相同。</p>
<h3 id="4-2-使用实例-1"><a href="#4-2-使用实例-1" class="headerlink" title="4.2 使用实例"></a><font size=3>4.2 使用实例</font></h3><p>后边学习标准IO会再详细学习。</p>
<h2 id="5-sscanf"><a href="#5-sscanf" class="headerlink" title="5. sscanf "></a><font size=3>5. sscanf </font></h2><h3 id="5-1-函数说明-1"><a href="#5-1-函数说明-1" class="headerlink" title="5.1 函数说明"></a><font size=3>5.1 函数说明</font></h3><p>在 linux 下可以使用 man 3 sscanf 命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sscanf</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *str, <span class="type">const</span> <span class="type">char</span> *format, ...)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数从字符串读取格式化输入，表示我们可以通过此函数从一个字符串中格式化的读取我们要的格式数据。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>str ： char * 类型，是格式化输入数据的来源。</li>
<li>format ： char * 类型，为格式化字符串，与 scanf 一致。</li>
</ul>
<p><strong>【返回值】</strong> int 类型，如果成功，该函数返回成功匹配和赋值的个数。如果到达文件末尾或发生读错误，则返回  EOF 。</p>
<p><strong>【使用格式】</strong>一般情况下基本使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> buff[<span class="number">100</span>] = <span class="string">&quot;2022-5-4&quot;</span>;</span><br><span class="line"><span class="type">int</span> syear, smonth, sday;</span><br><span class="line"><span class="built_in">sscanf</span>(buff,<span class="string">&quot;%d-%d-%d&quot;</span>,&amp;syear, &amp;smonth, &amp;sday);</span><br></pre></td></tr></table></figure>

<p><strong>【注意事项】</strong> none </p>
<h3 id="5-2-使用实例-1"><a href="#5-2-使用实例-1" class="headerlink" title="5.2 使用实例"></a><font size=3>5.2 使用实例</font></h3><p>后边学习标准IO会再详细学习。</p>
<h1 id="三、输出可以带颜色？"><a href="#三、输出可以带颜色？" class="headerlink" title="三、输出可以带颜色？"></a><font size=3>三、输出可以带颜色？</font></h1><h2 id="1-ANSI-控制码简介"><a href="#1-ANSI-控制码简介" class="headerlink" title="1. ANSI 控制码简介"></a><font size=3>1. ANSI 控制码简介</font></h2><p>ANSI 控制码用于在字符显示系统中控制光标移动和字符色彩等，常用于 BBS 系统中。 ANSI ESCAPE SEQUENCES 又称为 VT100 系列控制码，国内译为 ANSI 控制码。 <strong>ANSI 控制码依赖终端，不是依赖语言</strong>，所以在 shell ， perl ， C 里应用都是可以的。</p>
<p><strong>ANSI 控制码开始的标志</strong>都为 ESC[ ， ESC 对应 ASCII 码表的 033 (八进制)。</p>
<h2 id="2-ANSI-控制码"><a href="#2-ANSI-控制码" class="headerlink" title="2. ANSI 控制码"></a><font size=3>2. ANSI 控制码</font></h2><p>这里只写了一部分，还有一些其他的控制码，这里就不列出了。</p>
<table>
    <tr>
        <td align="center">\033[0m</td>
        <td align="left">关闭所有属性 </td>
    </tr>
    <tr>
        <td align="center">\033[1m</td>
        <td align="left">设置高亮度</td>
    </tr>
    <tr>
        <td align="center">\033[4m</td>
        <td align="left">下划线 </td>
    </tr>
    <tr>
        <td align="center">\033[5m</td>
        <td align="left">闪烁 </td>
    </tr>
    <tr>
        <td align="center">\033[7m</td>
        <td align="left">反显 </td>
    </tr>
    <tr>
        <td align="center">\033[8m</td>
        <td align="left">消隐 </td>
    </tr>
    <tr>
        <td align="center">\033[30m ~ \033[37m</td>
        <td align="left">设置前景色（字体色）<br>30:黑 31:红 32:绿 33:黄 34:蓝 35:紫 36:深绿 37:白</td>
    </tr>
    <tr>
        <td align="center">\033[40m ~ \033[47m</td>
        <td align="left">设置背景色<br>40:黑 41:红 42:绿 43:黄 44:蓝 45:紫 46:深绿 47:白</td>
    </tr>
    <tr>
        <td align="center">\033[nA</td>
        <td align="left">光标上移n行</td>
    </tr> 
    <tr>
        <td align="center">\033[nB</td>
        <td align="left">光标下移n行</td>
    </tr> 
    <tr>
        <td align="center">\033[nC</td>
        <td align="left">光标右移n行 </td>
    </tr>
    <tr>
        <td align="center">\033[nD</td>
        <td align="left">光标左移n行 </td>
    </tr>
    <tr>
        <td align="center">\033[y;xH</td>
        <td align="left">设置光标位置，将光标移动到y行x列的位置 </td>
    </tr>
    <tr>
        <td align="center">\033[2J</td>
        <td align="left">清屏，光标移到左上角 </td>
    </tr>
    <tr>
        <td align="center">\033[K</td>
        <td align="left">清除从光标到行尾的内容 </td>
    </tr>
    <tr>
        <td align="center">\033[s</td>
        <td align="left">保存光标位置 </td>
    </tr>
    <tr>
        <td align="center">\033[u</td>
        <td align="left">恢复光标位置 </td>
    </tr>
    <tr>
        <td align="center">\033[?25l</td>
        <td align="left">隐藏光标 </td>
    </tr>
    <tr>
        <td align="center">\033[?25h</td>
        <td align="left">显示光标</td>
    </tr>
</table>
**【显示字符属性控制】**

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[n1;n2;......m</span><br></pre></td></tr></table></figure>

<p>设定显示字符的属性状态。若有两个以上设定则以<strong>分号</strong>将代码隔开。除非重新设定，否则原设定之属性一直被保留，若想后边的不受影响，可以在结束后加一个 \033[0m 。</p>
<h2 id="3-ANSI-码应用格式"><a href="#3-ANSI-码应用格式" class="headerlink" title="3. ANSI 码应用格式"></a><font size=3>3. ANSI 码应用格式</font></h2><h3 id="3-1-printf使用格式"><a href="#3-1-printf使用格式" class="headerlink" title="3.1 printf使用格式"></a><font size=3>3.1 printf使用格式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;\033[颜色值m 文本\n&quot;</span>)  <span class="comment">/* 最后加上\033[0m可以使其不影响其他字体 */</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-使用实例"><a href="#3-2-使用实例" class="headerlink" title="3.2 使用实例"></a><font size=3>3.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 前景色(字体颜色)，printf 打印输出的颜色定义，主要是用于实现带颜色的输出 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CLS           <span class="string">&quot;\033[0m&quot;</span>    <span class="comment">/* 清除所有颜色 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLACK         <span class="string">&quot;\033[1;30m&quot;</span> <span class="comment">/* 黑色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> RED           <span class="string">&quot;\033[1;31m&quot;</span> <span class="comment">/* 红色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GREEN         <span class="string">&quot;\033[1;32m&quot;</span> <span class="comment">/* 绿色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> YELLOW        <span class="string">&quot;\033[1;33m&quot;</span> <span class="comment">/* 黄色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLUE          <span class="string">&quot;\033[1;34m&quot;</span> <span class="comment">/* 蓝色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PURPLE        <span class="string">&quot;\033[1;35m&quot;</span> <span class="comment">/* 紫色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CYAN          <span class="string">&quot;\033[1;36m&quot;</span> <span class="comment">/* 青色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> WHITE         <span class="string">&quot;\033[1;37m&quot;</span> <span class="comment">/* 白色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BOLD          <span class="string">&quot;\033[1m&quot;</span>    <span class="comment">/* 加粗字体 */</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(RED<span class="string">&quot;qidaink\n&quot;</span>CLS);</span><br><span class="line">	<span class="built_in">printf</span>(GREEN<span class="string">&quot;qidaink\n&quot;</span>CLS);</span><br><span class="line">	<span class="built_in">printf</span>(BLUE<span class="string">&quot;qidaink\n&quot;</span>CLS);</span><br><span class="line">	<span class="built_in">printf</span>(CYAN<span class="string">&quot;qidaink\n&quot;</span>CLS);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\033[1;41;36mqidaink\033[0m\n&quot;</span>);</span><br><span class="line">	<span class="built_in">printf</span>(WHITE<span class="string">&quot;qidaink\n&quot;</span>CLS);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/b9a7171d.html">LV01-03-C语言-输入与输出</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年01月12日 - 16:29</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/b9a7171d.html" title="LV01-03-C语言-输入与输出">https://sumumm.github.io/post/b9a7171d.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV01-C语言基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/d7b81674.html" rel="prev" title="LV01-04-C语言-控制语句">
                  <i class="fa fa-angle-left"></i> LV01-04-C语言-控制语句
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/ce2c2639.html" rel="next" title="LV01-02-C语言-变量与运算符">
                  LV01-02-C语言-变量与运算符 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
